package com.snailvr.tv.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.util.Util;
import com.snail.readyvr.R;
import com.snailvr.tv.network.entity.JXListResponBean.DataBean.ListBean;
import com.snailvr.tv.ui.activity.PlayActivity;
import com.snailvr.tv.ui.view.MyProgressBar;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/19.
 */
public class JXDownloadAdapter extends RecyclerView.Adapter<JXDownloadAdapter.DownloadViewHolder> {
    private String url = "http://static.zqgame.com/html/playvideo.html?name=http://lom.zqgame.com/v1/video/LOM_Promo~2.flv";
    private Context mContext;
    private List<ListBean> mJXList;
    public JXDownloadAdapter(Context _context, List<ListBean> jxlist) {
        super();
        mContext = _context;
        mJXList = jxlist;
    }

    @Override
    public DownloadViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View _view = LayoutInflater.from(mContext).inflate(R.layout.layout_select_download_item, parent, false);
        final DownloadViewHolder mHolder = new DownloadViewHolder(_view);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(DownloadViewHolder holder, int position) {
        final ListBean _Bean = mJXList.get(position);
        if (Util.isOnMainThread()){
            Glide.with(mContext)
                    .load(_Bean.getThumb())
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(holder.mImageView);
        }
        holder.mTextViewName.setText(_Bean.getName());
        holder.mTextViewTime.setText(mContext.getResources().getString(R.string.desc) + ": " + _Bean.getDesc());
        if (_Bean.getId() == PlayActivity.mRoomInfoBean.getId()) {
            holder.mLayout.setBackgroundResource(R.drawable.shape_round_layout_5_blue);
            holder.mImageViewLive.setVisibility(View.VISIBLE);
            holder.mImageViewPlay.setVisibility(View.GONE);
        } else {
            holder.mImageViewLive.setVisibility(View.GONE);
            holder.mLayout.setBackgroundResource(0);
            holder.mImageViewPlay.setVisibility(View.VISIBLE);
        }

//        if (DLManager.getInstance().getDLInfo(_Bean.getDownloadurl()) == null) {
//            _Bean.setDownloadStatus(ListBean.DownloadStatus.UnDownLoad);
//        } else {
//            _Bean.setDownloadStatus(ListBean.DownloadStatus.Downloaded);
//        }

        ListBean.DownloadStatus _Status = _Bean.getDownloadStatus();
        if (_Status == ListBean.DownloadStatus.Downloaded) {
            holder.mButtonDownload.setVisibility(View.GONE);
            holder.mLayoutBuyDownload.setVisibility(View.GONE);
            holder.mButtonWatch.setVisibility(View.VISIBLE);
        } else if (_Status == ListBean.DownloadStatus.Downloading) {
            holder.mLayoutBuyDownload.setVisibility(View.GONE);
            holder.mButtonWatch.setVisibility(View.GONE);
            holder.mMyProgressBar.setVisibility(View.VISIBLE);
        } else if (_Status == ListBean.DownloadStatus.UnDownLoad) {
            holder.mButtonDownload.setVisibility(View.GONE);
            holder.mButtonWatch.setVisibility(View.GONE);
            holder.mLayoutBuyDownload.setVisibility(View.VISIBLE);
        }
        holder.mButtonDownload.setTag(_Bean.getDownloadurl());
        holder.mButtonDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = (String)v.getTag();
//                DLManager.getInstance().dlStart(url);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mJXList.size();
    }

    class DownloadViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.layout_root)
        RelativeLayout mLayout;
        @BindView(R.id.img_view_poster)
        ImageView mImageView;
        @BindView(R.id.txt_view_video_name)
        TextView mTextViewName;
        @BindView(R.id.img_view_play_button)
        ImageView mImageViewPlay;
        @BindView(R.id.img_view_live)
        ImageView mImageViewLive;
        @BindView(R.id.txt_view_video_desc)
        TextView mTextViewTime;
        @BindView(R.id.id_button_video_charge)
        Button mButtonBuy;
        @BindView(R.id.id_button_download)
        Button mButtonDownload;
        @BindView(R.id.id_layout_buy_download)
        RelativeLayout mLayoutBuyDownload;
        @BindView(R.id.id_myprogressbar)
        MyProgressBar mMyProgressBar;
        @BindView(R.id.id_button_watch)
        Button mButtonWatch;
        public DownloadViewHolder(View _view) {
            super(_view);
            ButterKnife.bind(this,_view);
        }
    }

    private static String generateTime(long utc) {
        Date mDate = new Date(utc);
        SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
         return  fmt.format(mDate);

    }

}
